package com.tina.controller;


import com.tina.entity.TiMenu;
import com.tina.entity.TiRole;
import com.tina.entity.TiUsers;
import com.tina.entity.vo.TiMenuLeftVo;
import com.tina.entity.vo.TiMenuVo;
import com.tina.model.ResultFormat;
import com.tina.model.ResultFormatPaging;
import com.tina.service.TiMenuService;
import com.tina.util.ResultPagingUtil;
import com.tina.util.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/tiMenu/")
public class TiMenuController {
    @Autowired
    private TiMenuService tiMenuService;

    private Logger logger = LoggerFactory.getLogger(TiMenuController.class);

    /**
     * 条件查询菜单
     *
     * @param tiMenu
     * @tiPermission
     */
    @GetMapping("queryMenuList")
    @ApiOperation(value = "条件查询菜单")
    public ResultFormatPaging queryMenuList(TiMenu tiMenu) {
        logger.info("tiMenu={}", tiMenu);
        List<TiMenu> tiMenuList = tiMenuService.queryMenuList(tiMenu);
        return ResultPagingUtil.pagingSuccess(0, (long) tiMenuList.size(), tiMenuList);
    }

    /**
     * 通过菜单id
     * 查询上级菜单
     * 查询菜单权限
     *
     * @param menuId
     * @return
     */
    @GetMapping("queryMenuByMenuId")
    @ApiOperation(value = "菜单id查询菜单信息")
    public ResultFormat queryMenuByMenuId(Long menuId) {
        logger.info("menuId={}", menuId);
        return ResultUtil.success(tiMenuService.queryMenuByMenuId(menuId));
    }


    /**
     * 修改 菜单 状态
     *
     * @param tiMenu
     * @return
     */
    @PutMapping("updateMenuState")
    @ApiOperation(value = "修改菜单状态")
    public ResultFormat updatePermissionState(TiMenu tiMenu) {
        logger.info("tiMenu={}", tiMenu);
        if (tiMenu != null) {
            if (tiMenu.getMenuId() != null && tiMenu.getState() != null) {
                boolean updateById = tiMenuService.updateById(tiMenu);
                if (updateById) {
                    return ResultUtil.success();
                }
            }
        }
        return ResultUtil.error();
    }

    /**
     * 查询菜单转树形
     *
     * @tiPermission
     */
    @GetMapping("queryMenuTree")
    @ApiOperation(value = "查询菜单转树形")
    public ResultFormat queryMenuTree() {
        List<TiMenuVo> allMenus = tiMenuService.getAllMenus();
        return ResultUtil.success(allMenus);
    }

    /**
     * 查询菜单转树形
     * 首页左边菜单展示
     * 过滤按钮  F
     *
     * @tiPermission
     */
    @GetMapping("queryMenuLeftTree")
    @ApiOperation(value = "查询菜单转树形")
    public List<TiMenuLeftVo> queryMenuLeftTree() {
        Subject subject = SecurityUtils.getSubject();
        TiUsers tiUsersSubject = (TiUsers) subject.getPrincipal();
        if (tiUsersSubject != null) {
            List<TiRole> tiRoleList = tiUsersSubject.getTiRoleList();
            List<Integer> tableNames = tiRoleList.stream().map(TiRole::getRoleId).collect(Collectors.toList());
            List<TiMenuLeftVo> tiMenuLeftVos = null;
            for (Integer roleId : tableNames) {
                logger.info("roleId={}", roleId);
                tiMenuLeftVos = tiMenuService.queryMenuLeftTreeByRoleId(roleId);
            }
            if (tiMenuLeftVos != null) {
                return tiMenuLeftVos;
            }
        }
        return null;
    }

    /**
     * 添加菜单
     *
     * @tiPermission
     */
    @PostMapping("saveMenu")
    @ApiOperation(value = "添加菜单")
    public ResultFormat saveMenu(TiMenu tiMenu, Long select) {
        logger.info("tiMenu={}", tiMenu);
        logger.info("select={}", select);
        Integer save = tiMenuService.saveMenu(tiMenu, select);
        logger.info("save={}", save);
        return ResultUtil.success(save, "");
    }


    /**
     * 编辑菜单
     *
     * @tiPermission
     */
    @PostMapping("editMenu")
    @ApiOperation(value = "编辑菜单")
    public ResultFormat editMenu(TiMenu tiMenu, Long select) {
        logger.info("tiMenu={}", tiMenu);
        logger.info("select={}", select);
        Integer save = tiMenuService.editMenu(tiMenu, select);
        logger.info("save={}", save);
        return ResultUtil.success(save, "");
    }

    /**
     * 删除 菜单 把菜单的状态设置为 2
     * 0 显示
     * 1 隐藏
     * 2 删除
     * 判断 是否是父菜单
     * 主有是子菜单才能设置状态为 2
     *
     * @param menuId
     * @return 101 存在子菜单,不允许删除
     * 102 菜单已分配,不允许删除
     * 100 删除失败
     * 200 删除成功
     */
    @DeleteMapping("delMenu")
    @ApiOperation(value = "删除菜单")
    public ResultFormat delMenu(Long menuId) {
        logger.info("menuId={}", menuId);
        Integer delMenuNum = tiMenuService.delMenu(menuId);
        logger.info("delMenuNum={}", delMenuNum);
        return ResultUtil.success(delMenuNum, "");
    }

}
